package com.wmyg.antifake.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

public class FileUtil {

    /**
     * 读取数据流生成图片
     * @param imageIo
     * @param path
     * @return
     */
    public boolean base64ToImage(String imageIo, String path){
        boolean imageFlag = false;
        FileOutputStream fileOutputStream = null;
        try {
            //把加密的图片流解密成字符
            byte[] faceBytes = Base64.decodeBase64(imageIo);
            // 生成图片
            File file = new File(path);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            fileOutputStream = new FileOutputStream(path);
            IOUtils.write(faceBytes, fileOutputStream);
            imageFlag = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(fileOutputStream);
        }
        return imageFlag;
    }

    /**
     * 上传图片
     * @param requests
     * @return
     */
    public static Map<String,Object> upload(MultipartHttpServletRequest requests,String filePath,String staticAccessPath,String uploadBasePath){
        Map<String,Object> baseResult = new HashMap<>();
        Iterator<String> itr =  requests.getFileNames();
        MultipartFile mpf = null;
        //上传路径
        //String filePath =  "/static/pic/product/";
        while(itr.hasNext()){
            mpf = requests.getFile(itr.next());
            String fileName = mpf.getOriginalFilename();
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            String newFileName = UUID.randomUUID().toString()+suffixName;
            String fileSavePath=filePath+newFileName;
            String picAccessPath=staticAccessPath+filePath+newFileName;
            try {
                // 生成图片
                File file = new File(uploadBasePath+fileSavePath);
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }




                mpf.transferTo(file);
                BufferedImage sourceImg = ImageIO.read(new FileInputStream(file));
                int width = sourceImg.getWidth(); // 源图宽度
                int height = sourceImg.getHeight(); // 源图高度
                baseResult.put("picPath",fileSavePath);
                baseResult.put("picAccessPath",picAccessPath);
                baseResult.put("success","yes");
                baseResult.put("message","图片上传成功！");
                baseResult.put("width",width);
                baseResult.put("height",height);
            } catch (IOException e) {
                e.printStackTrace();
                baseResult.put("success","no");
                baseResult.put("message","上传图片失败！");
            }
        }
        return baseResult;
    }


    /**
     * 删除图片
     * @param url
     * @return
     */
    public static  int deletePic(String url){
        if(StringUtils.isEmpty(url)){
            return 0;
        }
        File file = new File(url);
        if (file.exists() && file.isFile() && file.delete())
            return 1;
        else
            return 0;
    }
}
